<div id="sidemenu" class="column-left ui-widget ui-widget-content ui-corner-all">
    <ul>
        <li <?php if ($page == 'inbox') {
            echo 'class="selected"';
            } ?>><?php echo $ajax->link('Inbox', '/messages/display/inbox', array('indicator' => 'indicator', 'update' => 'content')); ?></li>
        <li <?php if ($page == 'sent') {
            echo 'class="selected"';
            } ?>><?php echo $ajax->link('Sent', '/messages/display/sent', array('indicator' => 'indicator', 'update' => 'content')); ?></li>
    </ul>
</div>

<div class="column-content">

    <?php //echo $this->element('displayHeader'); ?>

    <?php
    $read_only = $session->read('Auth.User.read_only');
    if ($read_only == true) {
        $this->requestAction('app/session_auth_user_field/read_only/0');
    } ?>
    <div class="toolbar"><?php echo $this->element('toolbar', array('buttons' => array(array('add' => array('text' => 'New')),  array('search' => array('params' => $page)), 'refresh'))); ?></div>
    <?php if ($read_only == true) {
        $this->requestAction('app/session_auth_user_field/read_only/1');
    } ?>
    <?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <div class="fail-flash">Empty.</div>
    <? else : // ถ้ามีข้อมูล ?>

        <?php //echo $this->element('pageHeader'); ?>
        <?php $cur = $paginator->counter(array('format' => '%start%')); ?>

    <table class="data">
        <thead>
            <tr>
                <th class="no">No.</th>
                <th>Send to</th>
                <th>From</th>
                <th>Subject</th>
                <th width="150px">Sent time</th>
                <th class="action">&nbsp;</th>
            </tr>
        </thead>
        <tbody>
                <?php foreach($this->data as $key => $data): ?>
                    <?php
                    $tableCell = array();
                    $tableCell[] = $cur++;
                    if ($data['MessageSent']['receiver'] == ITS_PREFIX) {
                        $tableCell[] = $data['MessageSent']['send_to'] . ' @ ' . $this->requestAction('system_preferences/get/company_name');
                    } elseif ($data['MessageSent']['receiver'] == CUST_PREFIX) {
                        $tableCell[] = $data['MessageSent']['send_to'] . ' @ ' . $data['ToCustomer']['name'];
                    } elseif ($data['MessageSent']['receiver'] == SUPPLIER_PREFIX) {
                        $tableCell[] = $data['MessageSent']['send_to'] . ' @ ' . $data['ToSupplier']['name'];
                    }
                    $tableCell[] = $data['User']['name'];
                    $tableCell[] = $data['MessageSent']['subject'];
                    $tableCell[] = $html->tag('div', $data['MessageSent']['sent_time'], array('class' => 'center'));
                    $tableCell[] = $ajax->link(
                            $html->image('icon/edit.png', array('title' => 'view', 'alt' => 'view')), 'view/' . $data['MessageSent']['id'] . '/' . $page,
                            array('indicator' => 'indicator', 'update' => 'content'),   // เปลี่ยนหน้า
                            null, false
                    );
                    echo $html->tableCells($tableCell, array('class' => 'odd-cell'), array('class' => 'even-cell'));
                    ?>
                <?php endforeach; ?>
        </tbody>
    </table>
        <?php echo $this->element('paging'); ?>
    <? endif; ?>

</div>

<script type="text/javascript">
    $j(function() {
        $j("#sidemenu ul li").handover().bind('click', function(event) {
            var target = $j(event.target);
            if(target.is('li')) {
                $j('#content').update($j('a', target).attr('href'));
            }
        });
    });
</script>
